package com.china08.yxyapi.api.home.kecheng.sync;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSynchroCatalog;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSynchroGrade;
import com.china08.yxyapi.entity.db.res.YxyResSubject;
import com.china08.yxyapi.entity.db.res.YxyResVersion;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLessonsSynchroCatalogRepository;
import com.china08.yxyapi.repository.db.YxyLessonsSynchroGradeRepository;
import com.china08.yxyapi.repository.db.YxyResSubjectRepository;
import com.china08.yxyapi.repository.db.YxyResVersionRepository;

/**
 * 同步课程--列表页--通过学段筛选学科、版本、分类
 * 
 * @author LiuWeidong
 *
 */
@CrossOrigin
@RestController
public class KechengSyncPhaceListController extends YxyApiBaseController {

    @Autowired
    private YxyLessonsSynchroCatalogRepository yxyLessonsSynchroCatalogRepository;

    @Autowired
    private YxyResSubjectRepository yxyResSubjectRepository;

    @Autowired
    private YxyResVersionRepository yxyResVersionRepository;

    /** 分类 */
    @Autowired
    private YxyLessonsSynchroGradeRepository yxyLessonsSynchroGradeRepository;

    @RequestMapping(value = "/api_yw/home/kecheng/sync/phace/list/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> precess(HttpServletRequest request, HttpServletResponse response,
            @RequestBody ReqModel model) {

        String stageId = model.stageId;

        // 学科
        List<YxyLessonsSynchroCatalog> yxyLessonsSynchroCatalogLst = yxyLessonsSynchroCatalogRepository
                .findBySubjectIdAll(stageId);
        List<RespModel> returnLst = new ArrayList<>();
        RespModel aa = new RespModel();
        aa.subjectId = "";
        aa.subjectName = "不限";
        aa.versionId = "";
        aa.versioName = "不限";
        aa.gradeId = "";
        aa.gradeName = "不限";
        returnLst.add(aa);
        for (YxyLessonsSynchroCatalog yxyLessonsSynchroCatalog : yxyLessonsSynchroCatalogLst) {
            RespModel model1 = new RespModel();
            model1.subjectId = yxyLessonsSynchroCatalog.getSubjectId();

            YxyResSubject yxyResSubjects = yxyResSubjectRepository.findById(model1.subjectId);
            model1.subjectName = yxyResSubjects.getName();

            returnLst.add(model1);
        }

        // 版本
        List<YxyLessonsSynchroCatalog> versionLst = yxyLessonsSynchroCatalogRepository.findByVersionIdAll1(stageId);
        for (YxyLessonsSynchroCatalog yxyLessonsSynchroCatalog : versionLst) {
            RespModel model1 = new RespModel();
            model1.versionId = yxyLessonsSynchroCatalog.getVersionId();

            YxyResVersion yxyResVersions = yxyResVersionRepository.findById(model1.versionId);
            model1.versioName = yxyResVersions.getName();
            returnLst.add(model1);
        }

        // 分类
        List<YxyLessonsSynchroCatalog> gradeLst = yxyLessonsSynchroCatalogRepository.findByGradeIdAll1(stageId);
        for (YxyLessonsSynchroCatalog yxyLessonsSynchroCatalog : gradeLst) {
            RespModel model1 = new RespModel();
            model1.gradeId = yxyLessonsSynchroCatalog.getGradeId();

            YxyLessonsSynchroGrade yxyLessonsSynchroGrades = yxyLessonsSynchroGradeRepository.findById(model1.gradeId);
            if (yxyLessonsSynchroGrades != null) {
                model1.gradeName = yxyLessonsSynchroGrades.getName();
            }

            returnLst.add(model1);
        }

        return returnLst;
    }

    /* 请求参数 */
    protected static class ReqModel extends YxyApiReqModel {

        // 学段id
        public String stageId;
    }

    /* 响应参数 */
    protected static class RespModel extends YxyApiRespModel {

        // 学科ID
        public String subjectId;
        // 学科名称
        public String subjectName;
        // 版本ID
        public String versionId;
        // 版本名称
        public String versioName;
        // 分类ID
        public String gradeId;
        // 分类名称
        public String gradeName;
    }
}
